package com.chinamobile.ln.ictenterprises.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.chinamobile.ln.ictenterprises.common.MainApplication;
import com.chinamobile.ln.ictenterprises.http.HttpSend;
import com.chinamobile.ln.ictenterprises.model.Step;

public class Utils
{

    public static MainApplication app = MainApplication.getInstance();

    public static boolean isWifi()
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) app
            .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
            && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI)
        {
            return true;
        }
        return false;
    }

    public static byte[] serialIn(Object obj)
    {
        if (obj == null)
        {
            return null;
        }
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try
        {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            return baos.toByteArray();
        }
        catch (Exception e)
        {
            return null;
        }
        finally
        {
            try
            {
                if (baos != null)
                    baos.close();
                if (oos != null)
                    oos.close();
            }
            catch (IOException e)
            {}
        }
    }

    public static Object serialOut(byte[] buf)
    {
        if (buf == null)
        {
            return null;
        }
        ByteArrayInputStream baos = null;
        ObjectInputStream ois = null;
        try
        {
            baos = new ByteArrayInputStream(buf);
            ois = new ObjectInputStream(baos);
            Object o = ois.readObject();
            if (o != null)
            {
                return o;
            }
            else
            {
                return null;
            }
        }
        catch (Exception e)
        {
            return null;
        }
        finally
        {
            try
            {
                if (baos != null)
                    baos.close();
                if (ois != null)
                    ois.close();
            }
            catch (IOException e)
            {}
        }
    }

    /**
     * 判断传入的字符串是否不为空
     * 
     * @param str
     * @return 传入的字符串不为空返回true，反之返回false
     */
    public static boolean isNotEmpty(String str)
    {
        if (str != null && !"".equalsIgnoreCase(str) && str.length() > 0
            && !"null".equalsIgnoreCase(str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /** 组装请求参数工具方法 **/
    public static Step paramsUtil(String url, String params)
    {
        Step step = new Step();
        step.setUrl(url);
        step.setStreamString(params);
        step.setEncoding("UTF-8");
        step.setRequestType(HttpSend.JSON_REQUEST);
        return step;
    }
}
